package com.noreg.parser;

import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.crypto.spec.OAEPParameterSpec;

import org.apache.commons.collections.list.NodeCachingLinkedList;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.beans.LinkBean;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.ParserException;

import com.mover.action.Article;
import com.mover.action.CommonPostAction;
import com.mover.util.StringUtil;
import com.noreg.interfaces.IParser;
import com.noreg.util.HttpUtil;

public class NoRegParser extends HttpUtil implements IParser {
	private static final String _URL = "http://www.hackdos.com/Article/4/42/Article_072_1.html";
	private static SimpleDateFormat df = new SimpleDateFormat("M月d日");
	@Override
	public void start() throws Exception {
		// TODO Auto-generated method stub
		parse();
	}
	
	private static final String prefix = "http://www.hackdos.com/Article/4/42/html/";
	private static final String suf = ".html";
	int _max = 0;
	private void parse() throws Exception {
		LinkBean linkBean = new LinkBean();
		linkBean.setURL(_URL);
		URL [] links =  linkBean.getLinks();
		for(URL link : links){
			String l = link.toString();
			if(l.indexOf("http://www.hackdos.com/Article/4/42/html/")==-1){
				continue;
			}
			l = l.replace(prefix, "").replace(suf, "");
			int now = Integer.parseInt(l);
			if(now > _max){
				_max = now;
			}
		}
		
		parsePost(prefix+_max+suf);
	}

	Parser p = new Parser();
	HasAttributeFilter f = new HasAttributeFilter("class","contenr");
	CommonPostAction postAction = new CommonPostAction("http://www.cnblogs.com/bbpress/services/metaweblog.aspx");
	private void parsePost(String string) throws Exception {
		 String con = readInputStream(new URL(string).openStream(), "gb2312");
		 p.setInputHTML(con);
		String tt = getTitleByTag(con).replace(" - 3G安全网(Www.HackDos.Com)", "");
		String today = Calendar.MONTH+"月"+Calendar.DAY_OF_MONTH+"日";
		if(tt.indexOf(df.format(new Date()))==-1){
			return;
		}
		System.out.println(tt);
		List<Node> nodes = parseList(p, f);
		if(nodes == null || nodes.isEmpty()){
			return;
		}
		con = nodes.get(0).toHtml().replace("本站<a target=\"_blank\" href=\"http://www.hackdos.com\" class=\"UBBWordLink\">免费</a>公布给大家.希望大家多多支持3G安全网<br/>有需要购买国内外高速稳定空间可登录</span><a href=\"http://idc.hackdos.com\"><span style=\"color: #ff0000\">http://idc.hackdos.com</span></a><span style=\"color: #ff0000\">或联系QQ46603<br/>需要扫备案的<a target=\"_blank\" href=\"http://www.hackdos.com\" class=\"UBBWordLink\">软件</a>可以到这下载<a href=\"http://www.hackdos.com/soft/waigua/308/html/8178.html\">http://www.hackdos.com/soft/waigua/308/html/8178.html</a></span></p>", "");
		Article a = new Article();
		a.setUser("sl514");
		a.setPwd("8802514");
		a.setTitle(tt);
		a.setBody(con);
		postAction.postArticle(a);
	}

	public static void main(String[] args){
		try {
			new NoRegParser().parse();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String getToday = "1";
	public void setGetToday(String getToday) {
		this.getToday = getToday;
	}
	
}
